package com.imti.service.impl;



import com.imti.bean.entity.Nurse;
import com.imti.mapper.NurseMapper;
import com.imti.service.NurseService;
import com.imti.util.PageBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class NurseServiceImpl implements NurseService {

    @Resource
    private NurseMapper nurseMapper;

    @Override
    public PageBean pageNurseInfo(Map<String, Object> map) {
        // 1、查询总条数
        Nurse nurse = (Nurse) map.get("nurse");
        PageBean pageBean = (PageBean) map.get("page");
        Integer pageLimt = (pageBean.getPage() == null) ? 5 : pageBean.getPage();
        Integer currNum = (pageBean.getCurrNo() == null) ? 1 : pageBean.getCurrNo();
        int totalCount = nurseMapper.getNurseCount(nurse);
        // 2、查询列表
        PageBean page = new PageBean(pageLimt, currNum);
        map.put("page", page);
        List<Nurse> list = nurseMapper.findNurseInfo(map);
        // 3、封装pagebean
        PageBean npage = new PageBean(pageLimt, totalCount, currNum, list, "/nurse/page", getUserParams(nurse));
        return npage;
    }

    @Override
    public Boolean saveNurse(Nurse nurse) {
        return nurseMapper.saveNurse(nurse);
    }

    /**
     * @comment 拼接user查询参数
     * @param nurse
     * @return
     * @version 1.0
     */
    private String getUserParams(Nurse nurse) {
        StringBuilder sb = new StringBuilder();
        if (StringUtils.isNotBlank(nurse.getName())) {
            sb.append("&userName=").append(nurse.getName());
        }
        if (StringUtils.isNotBlank(nurse.getState().toString())) {
            sb.append("&state=").append(nurse.getState());
        }
//
        return sb.toString();// &userName=34343&userState=1&beginTime=2020-01-03
    }
}
